// Copyright 2017 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "mdf_common.h"
#include "mlink.h"

#include "device_handle.h"
#include "mbedtls/base64.h"
#include "dlt645_port.h"
#include "dlt645_api_07.h"
#include "dlt645_api_97.h"

#include "device_controller.h"
#include "mb_app.h"

static const char *TAG = "device_handle";

// uint32_t _cid_to_d97_ruler_id(int cid){
// 	uint32_t ruler_id=0xFFFFFFFF;
// 	switch(cid){
// 		case DEVICE_CID_ENERGY:
// 		case DEVICE_CID_SLAVE_ENERGY:
// 			ruler_id = E_D97_RULER_TYPE_COMB_HAVE_POWER_TOTAL;
// 			break;
// 	}
// 	return ruler_id;

// }

// uint32_t _cid_to_d07_ruler_id(int cid, int phase){
// 	uint32_t ruler_id=0xFFFFFFFF;
// 	switch(cid){
// 		case DEVICE_CID_ENERGY:
// 		case DEVICE_CID_SLAVE_ENERGY:
// 			ruler_id = E_D07_RULER_TYPE_FORTH_HAVE_POWER_TOTAL;
// 		break;

// 		case DEVICE_CID_POWER:
// 		case DEVICE_CID_SLAVE_POWER:
// 			switch(device_get_mode()){
// 				case DEVICE_MODE_AIR_SWITCH_IMEGA:
// 					switch(phase){
// 						case 0:
// 							ruler_id = E_D07_IMEGA_RULER_TYPE_INSTANT_HAVE_POWER_RATE_TOTAL;
// 							break;
// 						case 1:
// 							ruler_id = E_D07_IMEGA_RULER_TYPE_INSTANT_PHASE_A_HAVE_POWER_RATE;
// 							break;
// 						case 2:
// 							ruler_id = E_D07_IMEGA_RULER_TYPE_INSTANT_PHASE_B_HAVE_POWER_RATE;
// 							break;
// 						case 3:
// 							ruler_id = E_D07_IMEGA_RULER_TYPE_INSTANT_PHASE_C_HAVE_POWER_RATE;
// 							break;
// 					}
// 				break;
			
// 				default:
// 					switch(phase){
// 						case 0:
// 							ruler_id = E_D07_RULER_TYPE_INSTANT_HAVE_POWER_RATE_TOTAL;
// 							break;
// 						case 1:
// 							ruler_id = E_D07_RULER_TYPE_INSTANT_PHASE_A_HAVE_POWER_RATE;
// 							break;
// 						case 2:
// 							ruler_id = E_D07_RULER_TYPE_INSTANT_PHASE_B_HAVE_POWER_RATE;
// 							break;
// 						case 3:
// 							ruler_id = E_D07_RULER_TYPE_INSTANT_PHASE_C_HAVE_POWER_RATE;
// 							break;
// 					}
// 				break;
// 			}
// 		break;
			
// 		case DEVICE_CID_VOLTAGE:
// 		case DEVICE_CID_SLAVE_VOLTAGE:
// 			switch(phase){
// 				case 0:
// 				case 1:
// 					ruler_id = E_D07_RULER_TYPE_PHASE_A_VOLT;
// 					break;
// 				case 2:
// 					ruler_id = E_D07_RULER_TYPE_PHASE_B_VOLT;
// 					break;
// 				case 3:
// 					ruler_id = E_D07_RULER_TYPE_PHASE_C_VOLT;
// 					break;
// 			}
// 		break;
// 		case DEVICE_CID_CURRENT:
// 		case DEVICE_CID_SLAVE_CURRENT:
// 			switch(phase){
// 				case 0:
// 				case 1:
// 					ruler_id = E_D07_RULER_TYPE_PHASE_A_ELEC;
// 					break;
// 				case 2:
// 					ruler_id = E_D07_RULER_TYPE_PHASE_B_ELEC;
// 					break;
// 				case 3:
// 					ruler_id = E_D07_RULER_TYPE_PHASE_C_ELEC;
// 					break;
// 			}
// 		break;

// 		case DEVICE_CID_PF:
// 		case DEVICE_CID_SLAVE_PF:
// 			switch(phase){
// 				case 0:
// 					ruler_id = E_D07_RULER_TYPE_POWER_RATE_FACTOR_TOTAL;
// 					break;
// 				case 1:
// 					ruler_id = E_D07_RULER_TYPE_PHASE_A_POWER_RATE_FACTOR;
// 					break;
// 				case 2:
// 					ruler_id = E_D07_RULER_TYPE_PHASE_B_POWER_RATE_FACTOR;
// 					break;
// 				case 3:
// 					ruler_id = E_D07_RULER_TYPE_PHASE_C_POWER_RATE_FACTOR;
// 					break;
// 			}
// 		break;
// 	}
// 	return ruler_id;
// }

mdf_err_t mlink_set_value(char *cid_str, void *arg)
{
	int ret=MDF_OK;
    int value = *((int *)arg);

	if(strcmp(DEVICE_CID_MODE, cid_str) == 0){
		device_set_mode(value);
	}
	else if(strcmp(DEVICE_CID_REBOOT_TIME, cid_str) == 0){
		device_set_reboot_time(value);
	}
	else if(strcmp(DEVICE_CID_REPORT_PERIOD, cid_str) == 0) {
		device_set_report_time(value);
	}
	else if(strcmp(DEVICE_CID_DO01, cid_str) == 0) {
		device_set_do_status(DEVICE_IO_DO01, value);
	}
	else if(strcmp(DEVICE_CID_DO02, cid_str) == 0) {
		device_set_do_status(DEVICE_IO_DO02, value);
	}
	else if(strcmp(DEVICE_CID_DO03, cid_str) == 0) {
		device_set_do_status(DEVICE_IO_DO03, value);
	}
	else if(strcmp(DEVICE_CID_DO04, cid_str) == 0) {
		device_set_do_status(DEVICE_IO_DO04, value);
	}
	else {
		MDF_LOGD("unkown cid: %s, value: %d", cid_str, value);
		ret = MDF_ERR_NOT_SUPPORTED;
	}

    return ret;
}

mdf_err_t mlink_get_value(char *cid_str, void *arg)
{
	int ret=0;
    int *value = (int *)arg;
	float *value_float = (float *)arg;

	if(strcmp(DEVICE_CID_MODE, cid_str) == 0){
		*value = device_get_mode();
	}
	else if(strcmp(DEVICE_CID_TID, cid_str) == 0){
		*value = DEVICE_TID;
	}
	else if(strcmp(DEVICE_CID_REBOOT_TIME, cid_str) == 0){
		*value = device_get_reboot_time();
	}
	else if(strcmp(DEVICE_CID_REPORT_PERIOD, cid_str) == 0) {
		*value = device_get_report_time();
	}
	else if(strcmp(DEVICE_CID_TEMPERATURE, cid_str) == 0) {
		*value_float = device_get_temperature();
	}
	else if(strcmp(DEVICE_CID_DO01, cid_str) == 0) {
		*value = device_get_do_status(DEVICE_IO_DO01);
	}
	else if(strcmp(DEVICE_CID_DO02, cid_str) == 0) {
		*value = device_get_do_status(DEVICE_IO_DO02);
	}
	else if(strcmp(DEVICE_CID_DO03, cid_str) == 0) {
		*value = device_get_do_status(DEVICE_IO_DO03);
	}
	else if(strcmp(DEVICE_CID_DO04, cid_str) == 0) {
		*value = device_get_do_status(DEVICE_IO_DO04);
	}
	else if(strcmp(DEVICE_CID_DI01, cid_str) == 0) {
		*value = device_get_di_status(DEVICE_IO_DI01);
	}
	else if(strcmp(DEVICE_CID_DI02, cid_str) == 0) {
		*value = device_get_di_status(DEVICE_IO_DI02);
	}
	else if(strcmp(DEVICE_CID_DI03, cid_str) == 0) {
		*value = device_get_di_status(DEVICE_IO_DI03);
	}
	else if(strcmp(DEVICE_CID_DI04, cid_str) == 0) {
		*value = device_get_di_status(DEVICE_IO_DI04);
	}
	else if(strcmp(DEVICE_CID_AI01, cid_str) == 0) {
		ret = device_get_ai_status(DEVICE_IO_AI01, value_float);
		*value_float = *value_float*2.004/120;
	}
	else if(strcmp(DEVICE_CID_AI02, cid_str) == 0) {
		ret = device_get_ai_status(DEVICE_IO_AI02, value_float);
		*value_float = *value_float*2.004/120;
	}
	else if(strcmp(DEVICE_CID_AI03, cid_str) == 0) {
		ret = device_get_ai_status(DEVICE_IO_AI03, value_float);
		*value_float = *value_float*2.004/120;
	}
	else if(strcmp(DEVICE_CID_BAT_VOLTAGE, cid_str) == 0) {
		ret = device_get_ai_status(DEVICE_IO_AI_BATTERY, value_float);
		*value_float = *value_float*0.002;// 电池电压 放大系数2
	}
	else if(strcmp(DEVICE_CID_BAT_LEVEL, cid_str) == 0) {
		ret = device_get_ai_status(DEVICE_IO_AI_BATTERY, value_float);
		*value_float = *value_float*0.002;// 电池电压 放大系数2
		*value_float = (*value_float-3.7) / (4.2-3.7) * 100.0; // 电压转化为百分比
		if(*value_float < 0){
			*value_float = 0;
		}
		if(*value_float > 100){
			*value_float = 100;
		}
	}
	else if(strcmp(DEVICE_CID_POWER_SOURCE, cid_str) == 0) {
		*value = device_get_power_source();
	}
	else {
		MDF_LOGD("unkown cid: %s", cid_str);
		ret = MDF_ERR_NOT_SUPPORTED;
	}

    return ret;
}
